﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestorSQL.DAO.FileManager;
using System.IO;

namespace GestorSQL.BLL
{
    public class BLLTextWriter : IDisposable
    {
        private StreamWriter writer = null;

        public string FilePath { get; private set; }
        public string FileNameWithOutExtesion { get; private set; }

        public BLLTextWriter(string path, string fileNameWithOutExtesion, bool overwrite)
        {
            TextFile textFile = new TextFile();
            writer = textFile.GetStreamWriter(path, fileNameWithOutExtesion, overwrite);
            writer.AutoFlush = true;
            FilePath = path;
            FileNameWithOutExtesion = fileNameWithOutExtesion;
        }

        public void WriteLine(string information)
        {
            writer.WriteLine(information);
        }

        public static BLLTextWriter GetInstanceWithTempFileName(bool overwrite)
        {
            return GetInstanceFromFileName(Path.GetTempFileName(), overwrite);
        }

        public static BLLTextWriter GetInstanceFromFileName(string fullFileName, bool overwrite)
        {
            FileInfo file = new FileInfo(fullFileName);
            string path = file.Directory.FullName;
            string fileNameWithOutExtesion = file.Name.Remove(file.Name.Length - file.Extension.Length);
            return new BLLTextWriter(path, fileNameWithOutExtesion, overwrite);
        }

        public void Close()
        {
            if (writer != null)
            {
                writer.Flush();
                writer.Close();
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (writer != null)
            {
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
        }

        #endregion
    }
}
